package controllers

import (
	"aaa123/cache"
	"aaa123/models"
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"math"
	"path/filepath"
	"strings"
	"time"
)

type PetController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *PetController) Add() {
	dogname := c.GetString("dogname")
	clooer := c.GetString("clooer")
	dog := models.Dog{
		DogName: dogname,
		Clooer:  clooer,
	}
	err := models.DB.Create(&dog).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *PetController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if header.Size > 3*1024*1024 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "图片不能小于3mb",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	liu := map[string]bool{
		".JPD": true,
		".EDF": true,
		".PNG": true,
	}
	yu := strings.ToTitle(filepath.Ext(header.Filename))
	if liu[yu] == false {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件格式不支持",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	defer file.Close()
	path := "static/img/" + header.Filename
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "上传成功",
			Data: "http://127.0.0.1:8080/" + path,
		}
		c.ServeJSON()
		return
	}
}
func (c *PetController) Limit() {
	query := models.DB.Model(models.Dog{})
	name := c.GetString("name")
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	offset := (page - 1) * size
	var count int64
	query.Count(&count)
	sum := int(math.Ceil(float64(size) / float64(count)))
	up := page - 1
	if up < 1 {
		up = 1
	}
	down := page + 1
	if down > sum {
		down = sum
	}
	var do []models.Dog
	err := query.Limit(size).Offset(offset).Find(&do).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		pageData := make(map[string]interface{})
		pageData["sum"] = sum
		pageData["do"] = do
		pageData["up"] = up
		pageData["down"] = down
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: pageData,
		}
		c.ServeJSON()
		return

	}
}
func (c *PetController) List() {
	var do []models.Dog
	err := models.DB.Find(&do).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		key := fmt.Sprintf("pet_list_%d", models.Dog{}.ID)
		rest, _ := cache.Rdb.Get(cache.Ctx, key).Result()
		if rest != "" {
			json.Unmarshal([]byte(rest), do)
		} else {
			ress, _ := json.Marshal(do)
			cache.Rdb.Set(cache.Ctx, key, ress, time.Hour*2)
		}
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: do,
		}
		c.ServeJSON()
		return
	}
}
func (c *PetController) Dilter() {
	id, _ := c.GetInt("id")
	var do []models.Dog
	err := models.DB.Where("id=?", id).First(&do).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "详情查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		key := fmt.Sprintf("pet_dilter_%d", models.Dog{}.ID)
		rest, _ := cache.Rdb.Get(cache.Ctx, key).Result()
		if rest != "" {
			json.Unmarshal([]byte(rest), do)
		} else {
			ress, _ := json.Marshal(do)
			cache.Rdb.Set(cache.Ctx, key, ress, time.Hour*2)
		}
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: do,
		}
		c.ServeJSON()
		return
	}
}
